package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.UniversityAgencyFeeDto;
import xyz.zhouzhaodong.crm.dto.UniversityContractDto;
import xyz.zhouzhaodong.crm.entity.UniversityContract;
import xyz.zhouzhaodong.crm.entity.UniversityContract;
import xyz.zhouzhaodong.crm.entity.UniversityContract;
import xyz.zhouzhaodong.crm.mapper.UniversityContractMapper;
import xyz.zhouzhaodong.crm.mapper.UniversityContractMapper;
import xyz.zhouzhaodong.crm.mapper.UniversityMapper;
import xyz.zhouzhaodong.crm.service.IUniversityContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 高校合同 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-22
 */
@Service
public class UniversityContractServiceImpl extends ServiceImpl<UniversityContractMapper, UniversityContract> implements IUniversityContractService {

    @Resource
    UniversityContractMapper universityContractMapper;

    @Resource
    UniversityMapper universityMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addUniversityContract(UniversityContractDto universityContractDto) {
        try {
            UniversityContract universityContract = dtoChange(universityContractDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            universityContract.setId(id);
            universityContract.setCreateUser(universityContractDto.getStaffId());
            universityContract.setCreateTime(new Date());
            universityContractMapper.insert(universityContract);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteUniversityContract(UniversityContractDto universityContractDto) {
        try {
            UniversityContract universityContract = new UniversityContract();
            universityContract.setId(universityContractDto.getId());
            universityContract.setUpdateUser(universityContractDto.getStaffId());
            universityContract.setUpdateTime(new Date());
            universityContract.setFlag(1);
            universityContractMapper.updateById(universityContract);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateUniversityContract(UniversityContractDto universityContractDto) {
        try {
            UniversityContract universityContract = dtoChange(universityContractDto);
            universityContract.setUpdateUser(universityContractDto.getStaffId());
            universityContract.setUpdateTime(new Date());
            universityContractMapper.updateById(universityContract);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectUniversityContract(UniversityContractDto universityContractDto) {
        Integer flag = universityContractDto.getFlag() == null ? 0 : universityContractDto.getFlag();
        if (universityContractDto.getPageNo() != 0 && universityContractDto.getPageSize() != 0){
            Page<UniversityContractDto> page = new Page<>(universityContractDto.getPageNo(), universityContractDto.getPageSize());
            universityContractDto.setFlag(flag);
            IPage<UniversityContractDto> list = universityContractMapper.selectListPage(page, universityContractDto);
            for(UniversityContractDto u : list.getRecords()){
                u.setUniversity(universityMapper.selectById(u.getUniversityId()));
            }
            return RestMessage.success(list);
        } else {
            QueryWrapper<UniversityContract> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("university_id", universityContractDto.getUniversityId());
            queryWrapper.eq("flag", flag);
            List<UniversityContract> list = universityContractMapper.selectList(queryWrapper);
            return RestMessage.success(list);
        }
    }

    private UniversityContract dtoChange(UniversityContractDto universityContractDto) {
        UniversityContract universityContract = new UniversityContract();
        universityContract.setId(universityContractDto.getId());
        universityContract.setUniversityId(universityContractDto.getUniversityId());
        universityContract.setContractTimeStart(universityContractDto.getContractTimeStart());
        universityContract.setContractTimeEnd(universityContractDto.getContractTimeEnd());
        universityContract.setCooperationPolicy(universityContractDto.getCooperationPolicy());
        universityContract.setCommissionRate(universityContractDto.getCommissionRate());
        universityContract.setContractStatus(universityContractDto.getContractStatus());
        universityContract.setChargeTeacher(universityContractDto.getChargeTeacher());
        universityContract.setRemark(universityContractDto.getRemark());
        universityContract.setFlag(universityContractDto.getFlag());
        return universityContract;
    }

}
